﻿@model Bricks.Site.Models.Purview.PurviewPathView

@{
    ViewBag.Title = "授权";
                    //Layout = "~/Views/Shared/_UserPage.cshtml";
}

<script type="text/javascript">
    $(function () {
        //当页面首次刷新的时候执行的事件
        initTable();
    });
    //实现对DataGird控件的绑定操作

    function initTable(queryData) {
        $('#purviewDataGrid').datagrid({   //定位到Table标签，Table标签的ID是myDataGrid
            //   ActionInfo是控制器，GetData是控制器中获取用户数据的一个Action
            url: '/Purview/GetSelectedOrgPurviewPathList?configType=' + $("#hConfigType").val(),   //指向后台的Action来获取当前用户的信息的Json格式的数据
            //title: '功能管理',  //标识
            iconCls: 'icon-bricks',
            //width: 'auto',
            //height: 380,
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            //pagination: true,
            //pageSize: 20,
            rownumbers: true,
            //sortName: 'ID',    //根据某个字段给easyUI排序
            //sortOrder: 'asc',
            remoteSort: false,
            idField: 'ID',
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: true,
            queryParams: queryData,  //异步查询的参数
            toolbar: '#purviewToolBar',
            columns: [[
                { title: '@Html.LabelFor(model => model.PurviewPath)', field: 'PurviewPath', width: 372, sortable: true }
            ]]
        });
    }
    var Purview = {
        //修改
        //Modify: function () {
        //    var arr = $('#myDataGrid').datagrid('getSelections');

        //    if (arr.length == 1) {
        //        //window.location.href = "/ActionInfo/Modify/" + arr[0].Id;
        //        ActionInfo.ModifyMethod(arr[0].ID);
        //        $("#myDataGrid").datagrid("reload");
        //    } else {
        //        $.messager.alert('操作提示', '请选择一条数据!', 'warning');
        //    }
        //    return false;
        //},

        //批量选择权限
        BatchChoose: function () {
            //var orgList = $('#selectedOrgDataGrid').datagrid('getRows');  //获取列表数据
            ////var orgListJson = JSON.stringify(orgList);  //转为json格式
            //var orgIDs = "";
            //if (orgList.length > 0) {
            //    for (var i = 0; i < orgList.length; i++) {
            //        if (orgIDs.length == 0)
            //            orgIDs = orgList[i].ID;
            //        else
            //            orgIDs = orgIDs + "," + orgList[i].ID;
            //    }
            //}
            Purview.MutualBatch("选择权限", "/Purview/ChoosePurview?configType=" + $("#hConfigType").val());  //弹出对话框
        },

        //批量
        MutualBatch: function (title, href) {

            //测试
            //$.EasyuiMsg($("#hConfigType").val());

            Purview.ModelDialogBatch({
                title: title, href: href, width: 815, maximizable: true,
                //handler: function () {
                //    $.ModelForm({
                //        validate: true, button: $(this),
                //        id: "#form-purview",
                //        url: "/Purview/ChoosePurviewPost",  //eg：Purview/ChooseOrg
                //        success: function (result) {
                //            $("#purviewDataGrid").datagrid("reload");  //刷新右边权限路径列表（实际运行疑问：为什么这个语句在下方，而却先执行呢？）
                //        }
                //    });
                //},
                onLoad: function () { Purview.FormInit(); }
            });
        },

        //实体窗口（批量）
        ModelDialogBatch: function (options) {
            if (options.dialogid) {
                options.dialogid = "id='" + options.dialogid + "'";
            }
            var buttons = [];
            if (!options.width) { options.width = "auto"; }
            if (!options.height) { options.height = "auto"; }

            buttons.push({
                text: "覆盖", iconCls: "icon-caution",
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#form-purview",
                        url: "/Purview/CoverPurviewPost?configType=" + $("#hConfigType").val(),
                        success: function (result) {
                            //$.EasyuiMsg(result);
                            $("#purviewDataGrid").datagrid("reload");
                        }
                    });
                }
            });
            buttons.push({
                text: "追加", iconCls: "icon-upload",
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#form-purview",
                        url: "/Purview/AppendPurviewPost?configType=" + $("#hConfigType").val(),
                        success: function (result) {
                            $("#purviewDataGrid").datagrid("reload");
                        }
                    });
                }
            });
            buttons.push({ text: "关闭", iconCls: "icon-no", handler: function () { $.ButtonCloseDialog(this); } });

            var dialog = $("<div " + options.dialogid + " />").dialog({
                title: options.title, href: options.href,
                width: options.width, height: options.height,
                toolbar: options.toolbar, content: options.content,
                modal: true, cache: false, collapsible: true,
                maximizable: options.maximizable,  //允许最大化
                buttons: buttons, iconCls: options.iconCls,
                onLoad: function () {
                    options.onLoad(); $.AdjustmentDialog(dialog);
                },
                onClose: function () { $(this).dialog("destroy"); }
            });
            return dialog;
        },

        //单独选择权限
        AloneChoose: function () {
            //var orgList = $('#selectedOrgDataGrid').datagrid('getRows');  //获取列表数据
            ////var orgListJson = JSON.stringify(orgList);  //转为json格式
            //var orgIDs = "";
            //if (orgList.length > 0) {
            //    for (var i = 0; i < orgList.length; i++) {
            //        if (orgIDs.length == 0)
            //            orgIDs = orgList[i].ID;
            //        else
            //            orgIDs = orgIDs + "," + orgList[i].ID;
            //    }
            //}

            var dataGridName = "#selectedOrgDataGrid";
            if ($("#hConfigType").val() == "UserPurview")
                dataGridName = "#selectedUserDataGrid";
            else if ($("#hConfigType").val() == "RolePurview")
                dataGridName = "#selectedRoleDataGrid";

            //var row = $("#selectedUserDataGrid").datagrid("getSelected");
            //var row = $("#selectedUserDataGrid").datagrid("getSelected");
            var row = $(dataGridName).datagrid("getSelected");
            if (row) {
                Purview.MutualAlone("选择权限", "/Purview/ChoosePurview?instanceID=" + row.ID + "&configType=" + $("#hConfigType").val(), row.ID, $("#hConfigType").val());  //弹出对话框
            }
            else {
                $.EasyuiMsg("请选择左侧列表中的一个机构");
            }
        },

        //弹窗初始化
        FormInit: function () {
        },

        //单个
        MutualAlone: function (title, href, instanceID, configType) {
            $.ModelDialog({
                title: title, href: href, width: 815, maximizable: true,
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#form-purview",
                        url: "/Purview/CoverPurviewPost?instanceID=" + instanceID + "&configType=" + configType,  //isAlone表示是否单个，单个时提示“操作成功”
                        success: function (result) {
                            $("#purviewDataGrid").datagrid("reload");  //刷新右边权限路径列表（实际运行疑问：为什么这个语句在下方，而却先执行呢？）
                        }
                    });
                },
                onLoad: function () { Purview.FormInit(); }
            });
        },

        //删除
        Delete: function () {
            var row = $("#purviewDataGrid").datagrid("getSelected");
            if (row) {
                var configType = $("#hConfigType").val();  //取得ConfigType
                if (configType != "UserPurview") {
                    $.post("/Purview/DeletePurview?instanceID=" + row.InstanceID + "&menuActionID=" + row.MenuActionID + "&configType=" + configType);  //xxxx由上面的方法获取
                }
                    //UserPurview需特殊处理，因为InstanceID需传UserOrgID，而row.Instance为UserID
                else {
                    var userOrgRow = $("#selectedUserDataGrid").datagrid("getSelected");
                    if (userOrgRow) {
                        $.post("/Purview/DeletePurview?instanceID=" + userOrgRow.ID + "&menuActionID=" + row.MenuActionID + "&configType=" + configType);
                    }
                }
                $.EasyuiSlide("删除成功，请点击确认提交至数据库");
                Purview.Reload();
            }
            else {
                $.EasyuiMsg("请选择一条记录");
            }
        },

        //刷新
        Reload: function () {
            $("#purviewDataGrid").datagrid("reload");
        }
    }
</script>
<table id="purviewDataGrid" style="height: 358px;"></table>
<!--数据列表（根据JS构造数据）-->
<div id="purviewToolBar">
    <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Purview.BatchChoose()' data-options='plain:true,iconCls:"icon-application_add"'>批量选择权限</a>
    <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Purview.AloneChoose()' data-options='plain:true,iconCls:"icon-archives"'>单独选择权限</a>
    <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Purview.Delete()' data-options='plain:true,iconCls:"icon-cancel"'>删除</a>
    <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Purview.Reload()' data-options='plain:true,iconCls:"icon-application_form_magnify"'>刷新</a>
</div>
